<?php
/**
 * 商品中心处理控制器
 * UserModel:LLW
 * Date:2020-03-04
 */

namespace app\caidongweb\controller;

use app\caidongweb\model\CategoryModel;
use app\caidongweb\model\GoodsModel;
use think\Request;
use think\Db;

class Goods extends BaseController
{
    //初始化程序
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->goods = new GoodsModel();
        $this->cate = new CategoryModel();
    }

    /******************************** 商品列表相关 ***************************************/
    /**
     * 首页
     * @return \think\response\View
     */
    public function goodList()
    {
        $list = $this->goods->getList();
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return view();
    }

    /**
     * 添加商品
     * @return array|\think\response\View
     */
    public function good_add()
    {
        if(input('post.')){
            $res = $this->goods->getAdd();
            return $res;
        }else{
            //商品分类
            $class = $this->cate->getList();
            $this->assign('class',$class);
            return view();
        }
    }

    /**
     * 编辑商品
     * @return \think\response\View
     */
    public function good_edit()
    {
        $id = input('id',null);
        if(input('post.')){
            $res = $this->goods->getEdit();
            return $res;
        }else{
            $find = Db::name('goods')->where('go_id',$id)->find();
            $find['go_imgs'] = array_filter(explode(',',$find['go_imgs']));
            $parent = Db::name('category')->where('ca_id',$find['go_caid'])->find();
            $class = $this->cate->getList();
            $this->assign('find',$find);
            $this->assign('parent',$parent);
            $this->assign('class',$class);
            return view();
        }
    }

    /**
     * 商品删除
     * @return array
     */
    public function goodDel()
    {
        $res = $this->goods->getDel();
        return $res;
    }
    /******************************** 商品分类相关 ***************************************/
    /**
     * 商品分类
     * @return \think\response\View
     */
    public function cateList()
    {
        $list = $this->cate->getList();
        $this->assign('list',$list);
        return view();
    }

    /**
     * 添加商品分类
     * @return array|\think\response\View
     */
    public function cate_add()
    {
        if(input('post.')){
            $res = $this->cate->getAdd();
            return $res;
        }else{
            //文章分类
            $class = $this->cate->getList();
            $this->assign('class',$class);
            return view();
        }
    }

    /**
     * 编辑商品分类
     * @return \think\response\View
     */
    public function cate_edit()
    {
        $id = input('id',null);
        if(input('post.')){
            $res = $this->cate->getEdit();
            return $res;
        }else{
            $find = Db::name('category')->where('ca_id',$id)->find();
            $this->assign('find',$find);
            return view();
        }
    }

    /******************************** 联动 ***************************************/
    /**
     * 联动
     * @return array
     */
    /*public function linkage(Request $request)
    {
        $opt = '<option>--请选择--</option>';
        if($request->isPost()){
            $ca_parent = $request->post('ca_parent');
            $go_caid = Db::name('category')->where('ca_parent',$ca_parent)->select();
            foreach ($go_caid as $k => $val){
                $opt .="<option value='$val->ca_id'>$val->ca_name</option>";
            }
        }
        echo json_encode($opt);
    }*/

    /******************************** 文件上传 ***************************************/
    /**
     * 图片上传
     * @return array
     */
    public function uploads()
    {
        $id = input('id');
        $file = \request()->file('file');
        $newname = time();
        if($id == 1){
            $path = 'uploads/goods_class/';
        }else{
            $path = 'uploads/goods/';
        }
        if(!file_exists($path)){
            mkdir($path,777);
        }
        $saveInfo = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move($path,$newname);
        if($saveInfo){
            $pathInfo = '/'.$path.$saveInfo->getSaveName();
            return ['code'=>1,'msg'=>'上传成功','src'=>$pathInfo];
        }else{
            return ['code'=>-1,'msg'=>'上传失败'];
        }
    }

    //多图上传
    public function upimg()
    {
        // 第一步 接收上传的图片资源
        $file = request()->file('img');
        if($file){
            // 第二步 验证并且上传到我们项目的/public/uploads/目录下
            $info = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'Uploads');
            // 第三步 判断图片是否上传成功
            if($info){
                // 第四步 成功上传后 获取上传信息
                $img_src = '/uploads/'.$info->getSaveName();
                echo $img_src; //返回ajax请求
            }else{
                // 第四步 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
    }

}